#include "SList.h"

void SLTPrint(SLTNode* phead)
{
	SLTNode* pcur = phead;
	while (pcur != NULL)
	{
		printf("%d -> ", pcur->data);
		pcur = pcur->next;
	}
	printf("NULL\n");
}

SLTNode* SLTBuyNode(SLTDataType x)
{
	SLTNode* newnode = (SLTNode*)malloc(sizeof(SLTNode));
	if (!newnode)
	{
		printf("malloc fail!");
		exit(1);
	}
	newnode->data = x;
	newnode->next = NULL;
	return newnode;
}

void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	SLTNode* newnode = SLTBuyNode(x);
	if (*pphead == NULL)
	{
		*pphead = newnode;

	}
	else
	{
		SLTNode* ptail = *pphead; 
		while (ptail->next)
		{
			ptail = ptail->next;
		}
		ptail->next = newnode;
	}
	
}


void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = SLTBuyNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

int SLTPop(SLTNode** phead)
{
	if (!(*phead))
	{
		return 1;
	}
	if (!(*phead)->next)
	{
		free(*phead);
		*phead = NULL;
		return 1;
	}
	return 0;
}


void SLTPopBack(SLTNode** phead)
{
	
	if (SLTPop(phead))
	{
		return;
	}
	SLTNode* pcur = *phead;

	// 由于 SLTPop 已经处理了空链表和单节点链表
    // 这里确保至少有两个节点，所以 pcur->next 不为 NULL
	while (pcur->next->next)
	{
		pcur = pcur->next;
	}
	free(pcur->next);
	pcur->next = NULL;

}

void SLPopFront(SLTNode** phead)
{
	if (SLTPop(phead))
	{
		return;
	}
	SLTNode* pcur = *phead;
	*phead = pcur->next;
	free(pcur);
}

void SLTPopX(SLTNode** phead, SLTDataType x)//删除指定位置
{
	if (x <= 0)
	{
		printf("位置%d无效，超出链表长度\n", x);
		return;
	}
	if (!(*phead))
	{
		return;
	}
	if (x == 1)
	{
		SLTNode* p = *phead;
		*phead = (*phead)->next;
		free(p);
		return;
	}
	SLTNode* pcur = *phead;
	int count = 1;
	while (pcur != NULL && count < x - 1)
	{
		pcur = pcur->next;
		count++;
	}
	if (!pcur || !pcur->next)
	{
		printf("位置%d无效，超出链表长度\n", x);
		return;
	}
	SLTNode* p1 = pcur->next;
	pcur->next = pcur->next->next;
	free(p1);
}

SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* pcur = phead;
	while (pcur)
	{
		if (pcur->data == x)
		{
			return pcur;
		}
		pcur = pcur->next;
	}
	return NULL;
}

void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(*pphead && pos);
	SLTNode* prev = *pphead;
	SLTNode* newnode = SLTBuyNode(x);
	if (prev == pos)
	{
		newnode->next = *pphead;
		*pphead = newnode;
		return;
	}
	while (prev->next != pos && prev->next != NULL)
	{
		prev = prev->next;
	}
	if (prev->next != pos)
	{
		printf("错误，链表没有该节点\n");	
		free(newnode);
		return;
	}
	prev->next = newnode;
	newnode->next = pos;
}

void SLTInsertAfter(SLTNode* pos, SLTDataType x)
{
	if (!pos)
	{
		printf("pos不能为空\n");
		return;
	}
	SLTNode* newnode = SLTBuyNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	if (!pos || !pphead || !*pphead)
	{
		return;
	}
	if (*pphead == pos)
	{
		SLTNode* p = *pphead;
		*pphead = (*pphead)->next;
		free(p);
		p = NULL;
		return;
	}
	SLTNode* prev = *pphead;

	while (prev != NULL && prev->next != pos)
	{
		prev = prev->next;
	}
	if (prev == NULL)
	{
		printf("错误，要删除的节点不在链表中\n");
		return;
	}
	prev->next = pos->next;
	free(pos);
	pos = NULL;
}


void SLTEraseAfter(SLTNode* pos)
{
	if (!pos || !pos->next)
	{
		return;
	}
	SLTNode* p = pos->next;
	pos->next = p->next;
	free(p);
	p = NULL;

}





